﻿using System;
using System.Collections.Generic;
using System.BusinessObjects.With;
using Blazing.BusinessObjects.Cms;
using Blazing.BusinessObjects.Queries;
using Blazing.BusinessObjects.Tagging;
using System.Web.Configuration;

namespace Blazing.WebFacade
{
    public enum BuildingInternalTagTypes
    {
        overview,
        features,
        gallery,
        rates,
        book
    }

    public static class UrlHelper
    {
        public static bool IsUsingISAPI
        {
            get
            {
                bool useIsapi = true;
                string raw = WebConfigurationManager.AppSettings["UseISAPI"];
                if (!string.IsNullOrEmpty(raw))
                {
                    useIsapi = bool.Parse(raw);
                }
                return useIsapi;
            }
        }

        public static string CheckUrl(string url)
        {
            string requestUrl = url;
            requestUrl = requestUrl == null ? string.Empty : requestUrl;
            requestUrl = string.Format("{0}{1}", requestUrl, requestUrl.EndsWith("/") ? "" : "/");

            if (!IsUsingISAPI)
            {
                requestUrl += "Default.aspx";
            }

            return requestUrl;
        }

        public static string FormatFriendly(string str)
        {
            Dictionary<string, string> rules = new Dictionary<string, string>();
            rules.Add(" ", "-");
            rules.Add("'","");
            rules.Add("(","");
            rules.Add(")","");
            rules.Add("--","-");
            rules.Add(".", "");

            With.Each(rules).Item(delegate(KeyValuePair<string,string> pair) {
                str = str.Replace(pair.Key, pair.Value);
            });

            return str.ToLower();
        }

        public static IList<Contentpage> BuildingPagesByTags(IList<TaxonomyTag> buildingTags, Site site)
        {
            IList<Contentpage> list = Contentpage.Search(QrySearchPagesByTaxonomyTags.Query(buildingTags, site));
            if (list == null)
                list = new List<Contentpage>();
            return list;            
        }
    }
}
